<?php
namespace app\model\v1\housing;

use crmeb\basic\BaseModel;
use think\Model;
use think\facade\Db;
use app\model\v1\sys\BsStreet;
use app\model\v1\sys\AnAppointment;
use app\model\v1\sys\AnAssociation;
use app\model\user\user;


class HousingSource extends BaseModel
{

	protected $autoWriteTimestamp = false;
		//房源关联配套设施
		public function HousingMatchingSourceAssociation ()
		{
			return $this->hasMany(HousingMatchingSourceAssociation::class, 'housing_source_id', 'id')->alias ('h')
				->join ('housing_matching_setting l','l.ids = h.matching_setting_id')
				->field ('h.*,l.*');
		}

		//eb_housing_matching_setting
		public function HousingOffice ()
		{
			return $this->hasMany(HousingOffice::class, 'housing_source_id', 'id');
		}
		//用户头像
		public function userAvatar ()
		{
			return $this->hasOne(user::class, 'uid', 'user_id')->field ('avatar');
		}//用户总分
		public function userscore ()
		{
			return $this->hasOne(AnAppointment::class, 'uid', 'user_id')->group ('uid')->field ('avg(score) as score');
		}//用户总分
		public function userlabel ()
		{
			return $this->hasMany(AnAssociation::class, 'u_id', 'user_id')->join ('label_comment l','l.id = label_comment_id')
				->where ('deltime',null)
				->group ('label_comment_id')
				->order ('count',"desc")
				->limit ('8')
				->field ('count(label_comment_id) as count,l.name,l.id');
		}
		//企业名称
		public function housingEnterprisesAuthentication ()
		{
			return $this->hasMany(HousingEnterprisesAuthentication::class, 'user_id', 'user_id');
		}

		//房源关联标签
		public function HousingHousingLabelAssociation ()
		{
			return $this->hasMany(HousingHousingLabelAssociation::class, 'housing_source_id', 'id')->alias ('h')
				->join ('housing_label l','l.ids = h.housing_label_id')
				->field ('h.*,l.*');
		}
		//客流
		public function HousingPassengerFlowCrowdAssociation ()
		{
			return $this->hasMany('housing_passenger_flow_crowd_association', 'matching_setting_id', 'id');
		}
		//街道
		public function street()
		{
			return $this->hasMany(BsStreet::class, 'STREET_CODE', 'region')->alias ('s')->join ('BsArea a','a.AREA_CODE = s.AREA_CODE')->join ('BsCity c','c.CITY_CODE = a.CITY_CODE')->join ('BsProvince p','p.PROVINCE_CODE = c.PROVINCE_CODE')->field ('s.*,a.*,c.*,p.*');
		}

		//商铺类型
		public function HousingShopType()
		{
			return $this->hasMany('housing_shop_type', 'id', 'shop_type');
		}

		//行业
		public function HousingIndustry()
		{
			return $this->hasMany('housing_industry', 'id', 'suitable_for_business_form');
		}
		//地铁
		public function MetroStation()
	{
		return $this->hasMany('housing_metro_station', 'id', 'metro_station_id');
	}
		public function  Housing_index($housing_source_id,$user=null)
		{
			$housing_info = $this->where('id', $housing_source_id)->find ();
			$Housing_collection = Db::name ('housing_collection')
				->where(function ($query) use ($housing_source_id,$user){
					$query->where('housing_collection_id', $housing_source_id);
					$query->where('user_id', $user);
				})
				->find ();
            if ($housing_info){
				$housing_info->Housing_collection='2';
				if ($Housing_collection){
					$housing_info->Housing_collection='1';
				}
				$housing_info->HousingMatchingSourceAssociation;
				$housing_info->HousingHousingLabelAssociation;
				$housing_info->HousingPassengerFlowCrowdAssociation;

				$housing_info->MetroStation;
				$housing_info->street;
				$housing_info->HousingShopType;

				$housing_info->HousingIndustry;
				$housing_info->userAvatar;
				$housing_info->userscore;
				$housing_info->userlabel;

				$housing_info->housingEnterprisesAuthentication;
	            $housing_info->HousingOffice;

				return $housing_info;
			}else{
				return $housing_info;

			}


		}
}